<template>
  <div class="app-container">
    <el-table v-loading="loading" :data="dataList" @selection-change="handleSelectionChange">
      <el-table-column
        v-for="(item, index) in managementList"
        :key="index"
        :label="item.paramName"
        :prop="item.paramName"
        align="center">
        <template slot-scope="scope">
          <span>{{scope.row[item.paramName]}}</span>
        </template>
      </el-table-column>
      <!--<el-table-column-->
        <!--v-for="(item, index) in dataList"-->
        <!--:key="index"-->
        <!--align="center">-->
        <!--&lt;!&ndash;<template slot-scope="scope">&ndash;&gt;-->
        <!--&lt;!&ndash;<span>this.getFirst(scope.row,index)</span>&ndash;&gt;-->
        <!--&lt;!&ndash;</template>&ndash;&gt;-->
        <!--{{this.getFirst(this.dataList[0],index)}}-->
      <!--</el-table-column>-->
    </el-table>

      <!--<pagination-->
    <!--v-show="total>0"-->
    <!--:total="total"-->
    <!--:page.sync="queryParams.pageNum"-->
    <!--:limit.sync="queryParams.pageSize"-->
    <!--@pagination="getList"-->
    <!--/>-->

    <!-- 添加或修改接口管理对话框 -->
  </div>
</template>

<script>
  import { getData} from "@/api/test/data";
  import { getParam} from "@/api/test/paramedit";

  export default {
    name: "data",
    data() {
      return {
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 接口管理表格数据
        managementList: [],

        dataList: [],
        // 弹出层标题
        title: "",
        interfaceId:0,
        // 是否显示弹出层
        open: false,

        update:0,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          interfaceId:0,
        },
        // 表单参数
        form: {},
        form2:{},
        // 表单校验
        rules: {
        },

      };
    },
    created() {
      this.interfaceId = this.$route.params.id;
      this.getList();
      this.getDataList();
    },

    methods: {

      /** 查询接口管理列表 */
      getList() {
        this.loading = true;
        if (this.interfaceId!=undefined) {
          this.queryParams.interfaceId=this.interfaceId;
          getParam(this.queryParams).then(response => {
            this.managementList = response.rows;
            // this.total = response.total;

            this.loading = false;
          });
        }

      },
      getDataList() {
        if (this.interfaceId!=undefined) {
          this.queryParams.interfaceId=this.interfaceId;
          getData(this.queryParams).then(response => {
            this.dataList = response;
            console.log(this.getFirst(response[0],1));
          });
        }

      },
      getFirst(obj,i){		//obj为我们的对象
        var j = 0;
        for(var key in obj){
          if (j==i) {
            return obj[key];
          }
          j++;
        }
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          paramName: null,
          type: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.update=1;
        this.open = true;
        this.title = "添加响应参数";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.update=2;
        this.title = "修改响应参数";
        this.form.paramName = 1;
        this.form.paramName = row.paramName;
        this.form.type = row.type;
        this.open = true;
        // const id = row.id || this.ids
        // getManagement(id).then(response => {
        //   this.form = response.data;
        //   this.open = true;
        //   this.title = "修改响应参数";
        // });
      },
      /** 修改参数操作 */
      handleEditParam(row) {
        this.$router.push("/management");
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.update==2) {
              this.form2.id = this.interfaceId;
              this.form2.paramName = this.form.paramName;
              this.form2.type = this.form.type;
              updateParam(this.form2).then(response => {
                this.$modal.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              if (this.update==1)
              {
                this.form2.id = this.interfaceId;
                this.form2.paramName = this.form.paramName;
                this.form2.type = this.form.type;
                addParam(this.form2).then(response => {
                  this.$modal.msgSuccess("新增成功");
                  this.open = false;
                  this.getList();
                });
              }
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        // const ids = row.id || this.ids;
        this.form2.id = this.interfaceId;
        this.form2.paramName = row.paramName;
        this.form2.type = row.type;
        console.log(this.form2);
        delParam(this.form2).then(response => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        });
        // this.$modal.confirm('是否确认删除参数名为"' + row.paramName + '"的数据项？').then(function() {
        //   console.log(this.form2);
        //   return delParam(this.form2);
        // }).then(() => {
        //   this.getList();
        //   this.$modal.msgSuccess("删除成功");
        // }).catch(() => {});
      },
      /** 导出按钮操作 */

    }
  };
</script>
